from abc import ABC, abstractmethod
from datetime import date
import pandas as pd
from app.services.btdata import get_bars   # 新增
from loguru import logger
class BaseStrategy(ABC):
    def __init__(self, codes: list[str], start: date, end: date):
        self.codes = codes
        self.start = start
        self.end = end
        self.bars = get_bars(codes, start, end)          # 先接返回值
        logger.debug(f"[DEBUG] type(bars) = {type(self.bars)}")
        logger.debug(f"[DEBUG] bars.columns = {self.bars.columns.tolist() if hasattr(self.bars, 'columns') else 'No columns'}",)
        # 如果缺列，强制补
        if 'date' not in self.bars.columns:
            self.bars['date'] = pd.Timestamp.now()
        if 'code' not in self.bars.columns:
            self.bars['code'] = codes[0] if codes else 'sz000001'
        self.bars = self.bars.set_index(['date', 'code']).sort_index()
        self.bars.columns = self.bars.columns.str.strip().str.lower()
        rename_map = {'trade_date':'date', 'symbol':'code'}
        self.bars = self.bars.rename(columns=rename_map)
        # raise NotImplementedError

    @abstractmethod
    def next(self, row: pd.Series) -> dict[str, float]:
        """返回 {code: weight}，权重和=1"""
        raise NotImplementedError
